package com.zebra.remoting.protocol;

import com.zebra.common.ZebraBaseMessage;
import com.zebra.remoting.config.StateCode;
import com.zebra.remoting.config.StateMessage;
import io.netty.channel.Channel;
import lombok.*;

@Data
@ToString(exclude={"commandModel", "response", "channel"})
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ZebraCommand<T extends ZebraBaseMessage> implements RemotingCommand {

    private  int id;
    private Channel channel;
    private int cmd; //命令号
    private int state = 200; // 状态
    private long token;  //token
    private long userId;  //userId
    private int gameId;  // 大厅消息传0
    private int reserve;  // 保留字段

    private T request;

    private ZebraBaseMessage response;

    private CommandModel commandModel;
    private boolean logs; //日志 true  不打印； 默认 false 打印日志

    public void sendMessage(ZebraBaseMessage message){
         writeAndFlush(message, 200);
    }

    public void sendSuccessMsg(){
        writeAndFlush(StateMessage.builder().state(StateCode.Success.code()).build(), 200);
    }

    public void sendFailureCode(int code){
        writeAndFlush( StateMessage.builder().state(code).build(), code);
    }

    public void writeAndFlush(ZebraBaseMessage message, int state){
        if (null == message){
            return;
        }
        this.state = state;
        this.response = message;
        channel.writeAndFlush(this);
    }

    @Override
    public ProcessorType getProcessorType() {
        return this.commandModel.getProcessorType();
    }

    @Override
    public PoolType getPoolType() {
        return this.commandModel.getPoolType();
    }

    @Override
    public CommandType getCommandType() {
        return this.commandModel.getCommandType();
    }

    @Override
    public int cmd() {
        return this.cmd;
    }

    @Override
    public int requestId() {
        return this.id;
    }

    @Override
    public int addRequestId(int id) {
        return this.id = id;
    }

}
